using System.Web.Mvc;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Attributes;
using good1.Web.Controllers.Infrastructure;
using good1.Web.Controllers.Infrastructure.Cookies;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
    public class EmailAddressesController : BaseController
    {
        public EmailAddressesController(IRepository<EmailAddress> emailAddressRepository, ISecurityContextService securityContext) : base(securityContext)
        {
            Check.Require(emailAddressRepository != null, "emailAddressRepository may not be null");

            this.emailAddressRepository = emailAddressRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<EmailAddress> emailAddresses = emailAddressRepository.GetAll();
            return View(emailAddresses);
        }

        [Transaction]
        public ActionResult Show(int id) {
            EmailAddress emailAddress = emailAddressRepository.Get(id);
            return View(emailAddress);
        }

        public ActionResult Create() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(EmailAddress emailAddress) {
            if (emailAddress.IsValid()) {
                emailAddressRepository.SaveOrUpdate(emailAddress);

                TempData["message"] = "The emailAddress was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                emailAddress.ValidationResults());
            return View();
        }

        [Transaction]
        [Authorize]
        public ActionResult Edit(int id) {
            EmailAddress emailAddress = emailAddressRepository.Get(id);
            return View(emailAddress);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] EmailAddress emailAddress) {
            EmailAddress emailAddressToUpdate = emailAddressRepository.Get(id);
            TransferFormValuesTo(emailAddressToUpdate, emailAddress);

            if (emailAddressToUpdate.IsValid()) {
                TempData["message"] = "The emailAddress was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                emailAddressRepository.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    emailAddressToUpdate.ValidationResults());
                return View(emailAddressToUpdate);
            }
        }

        private void TransferFormValuesTo(EmailAddress emailAddressToUpdate, EmailAddress emailAddressFromForm) {
            emailAddressToUpdate.Email = emailAddressFromForm.Email;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The emailAddress was successfully deleted.";
            EmailAddress emailAddressToDelete = emailAddressRepository.Get(id);

            if (emailAddressToDelete != null) {
                emailAddressRepository.Delete(emailAddressToDelete);

                try {
                    emailAddressRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the emailAddress from being deleted. " +
						"Another item likely depends on this emailAddress.";
                    emailAddressRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The emailAddress could not be found for deletion. It may already have been deleted.";
            }

            TempData["Message"] = resultMessage;
            return RedirectToAction("Index");
        }

        private readonly IRepository<EmailAddress> emailAddressRepository;
    }
}
